<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="getCompanyHonor"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option v-for="dict in dict.type.jiayi_about_us__company_honor_type" :key="dict.value" :label="dict.label" :value="dict.value">
            <el-tag :type="dict.raw.listClass">{{dict.label}}</el-tag>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getCompanyHonor">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="20" v-loading="loading">
       <el-col :span="4" v-for="honor in valueList">
         <el-badge :value="parse_dict(honor.type).label" :type="parse_dict(honor.type).raw.listClass" class="badge">
           <el-popover placement="bottom" trigger="hover" :content="honor.info">
             <Card slot="reference" width="100%" height="100%" border-radius="1rem" class="card" >
               <p class="name" @click="handleUpdate(honor)">{{ honor.name }}</p>
               <div class="photo">
                 <image-preview width="100%" height="15vh" :src="honor.photoUrl"/>
               </div>
             </Card>
           </el-popover>
         </el-badge>
       </el-col>
    </el-row>

    <el-drawer :title="title" :visible.sync="open" size="40%" append-to-body destroy-on-close :wrapperClosable="false">
      <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入名称" style="width: 30%" clearable/>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择类型" style="width: 30%" clearable>
              <el-option v-for="dict in dict.type.jiayi_about_us__company_honor_type" :key="dict.value" :label="dict.label" :value="dict.value">
                <el-tag :type="dict.raw.listClass">{{dict.label}}</el-tag>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时间" prop="date">
            <el-date-picker v-model="form.date" type="date" value-format="yyyy-MM-dd" style="width: 30%" placeholder="选择时间"/>
          </el-form-item>
          <el-form-item label="图片" prop="photoUrl">
            <ImageUpload :value="form.photoUrl" :limit="1" @input="handleUploadPhotoUrl"/>
          </el-form-item>
          <el-form-item label="详情" prop="info">
            <el-input type="textarea" :rows="3" v-model="form.info" placeholder="请输入详情" style="width: 50%" clearable/>
          </el-form-item>
        </el-form>
      </div>
      <div style="width: 100%;text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {getJiayi_about_us_company_honor, updateJiayi_about_us} from "@/api/cms/jiayi_about_us";

export default {
  name: "Jiayi_about_us__company_honor",
  dicts: ['jiayi_about_us__company_honor_type'],
  data() {
    return {
      loading: false,
      queryParams: {
        name: null,
        type: null,
      },
      typeDictMap: {},
      aboutUsForm: {},
      valueList: [],
      open: false,
      title: '',
      form: {},
      rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"},
        ],
        type: [
          {required: true, message: "请选择类型", trigger: "blur"},
        ],
        date: [
          {required: true, message: "请选择时间", trigger: "blur"},
        ],
        photoUrl: [
          {required: true, message: "请上传图片", trigger: "blur"},
        ],
      },
    };
  },
  computed:{
    parse_dict(type){
      return (type)=>{
        for (let i = 0; i < this.dict.type.jiayi_about_us__company_honor_type.length; i++) {
          let d = this.dict.type.jiayi_about_us__company_honor_type[i];
          if (d.value == type){
            return d;
          }
        }
      }
    }
  },
  created() {
    this.getCompanyHonor();
  },
  methods: {
    getCompanyHonor(){
      this.loading = true;
      getJiayi_about_us_company_honor(this.queryParams).then(response=>{
        this.aboutUsForm = response.data;
        this.valueList = response.data.valueObj;
        this.loading = false
      })
    },
    resetQuery(){
      this.queryParams = {
        name: null,
        type: null,
      };
      this.getCompanyHonor();
    },
    cancel(){
      this.open = false;
    },
    resetForm(){
      this.form = {
        id: null,
        type: null,
        name: null,
        date: null,
        photoUrl: null,
        info: null,
      };
    },
    handleAdd(){
      this.resetForm();
      this.title = '添加荣誉资质';
      this.open = true;
    },
    handleUpdate(row){
      console.log(1)
      this.title = '修改荣誉资质';
      this.resetForm();
      this.form = {...row};
      this.open = true;
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.aboutUsForm.valueObj = this.form;
          updateJiayi_about_us(this.aboutUsForm).then(response => {
            this.getCompanyHonor();
            this.$modal.msgSuccess("操作成功");
            this.cancel();
          });
        }
      });
    },
    handleDelete(row){
      this.$modal.confirm(`是否确认删荣誉资质【${row.name}】`).then(()=>{
        this.aboutUsForm.valueObj = row.id;
        return updateJiayi_about_us(this.aboutUsForm);
      }).then(() => {
        this.getCompanyHonor();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleUploadPhotoUrl(url){
      this.form.photoUrl = url;
    }
  }
};
</script>
<style scoped>
.badge{
  margin: 1rem 0 1.5rem 0;
  width: 80%;
  height: 22vh;
}
.badge /deep/.el-badge__content.is-fixed{
  right: 95% !important;
  height: 1.7rem;
  font-size: 1.1rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 2vw;
}
.card{
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: start;
  align-items: center;
  cursor: pointer;
}
.name{
  font-size: 1rem;
  font-weight: bold;
}
.photo{
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

</style>
